<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="Shipment ID" prop="shipmentId">
        <el-input
          v-model="queryParams.shipmentId"
          placeholder="请输入Shipment ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账号标记" prop="account">
        <el-input
          v-model="queryParams.account"
          placeholder="请输入账号标记"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['logistics:logisticsTracking:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['logistics:logisticsTracking:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['logistics:logisticsTracking:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['logistics:logisticsTracking:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['system:tracking:import']"
          >导入</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <div
      ref="tableContainer"
      class="table-container"
      @mousedown="_handleMouseDown"
      @mouseup="_handleMouseUp"
      @mouseleave="_handleMouseUp"
    >
      <vxe-table
        border
        show-overflow
        show-footer-overflow
        ref="xTable"
        height="600"
        :data="logisticsTrackingList"
        :loading="loading"
        :scroll-x="{ enabled: true }"
        :column-config="{ resizable: true }"
        :virtual-y-config="{ enabled: true, gt: 0 }"
        :virtual-x-config="{ enabled: true, gt: 0 }"
        :row-config="{ isHover: true }"
      >
        <vxe-column field="id" title="ID" width="160"></vxe-column>
        <vxe-column
          field="shipmentId"
          title="Shipment ID"
          width="100"
          sortable
        ></vxe-column>
        <vxe-column field="account" title="账号标记" width="160"></vxe-column>
        <vxe-column field="sendDate" title="发货日期" width="200"></vxe-column>
        <vxe-column
          field="volumeRatio"
          title="体积重除数"
          width="140"
        ></vxe-column>
        <vxe-column field="channel" title="渠道" width="300"></vxe-column>
        <vxe-column
          field="totalBoxNum"
          title="总箱数"
          width="160"
          sortable
        ></vxe-column>
        <vxe-column field="squareNum" title="方数" width="120"></vxe-column>
        <vxe-column field="weight" title="重量" width="400"></vxe-column>
        <vxe-column field="units" title="units" width="160"></vxe-column>
        <vxe-column field="price" title="货值" width="160"></vxe-column>
        <vxe-column
          field="warehouseCode"
          title="仓库代号"
          width="180"
        ></vxe-column>
        <vxe-column
          field="logisticsPrice"
          title="实际物流运费"
          width="160"
        ></vxe-column>
        <vxe-column
          field="aveUnitPrice"
          title="平均单价"
          width="80"
        ></vxe-column>
        <vxe-column field="sailDate" title="开船日期" width="120"></vxe-column>
        <vxe-column
          field="arrivalDate"
          title="到港日期"
          width="360"
        ></vxe-column>
        <vxe-column
          field="signForDate"
          title="签收时间"
          width="150"
        ></vxe-column>
        <vxe-column field="ageing" title="时效" width="380"></vxe-column>
        <vxe-column field="waybillNum" title="运单号" width="100"></vxe-column>
        <vxe-column
          field="paymentStatus"
          title="付款状态 1-已付款 2-未付款"
          width="290"
        ></vxe-column>
        <vxe-column field="shipsName" title="船名" width="80"></vxe-column>
        <vxe-column field="remark" title="备注" width="100"></vxe-column>
        <vxe-column
          field="groundingNum"
          title="上架数量"
          width="120"
        ></vxe-column>
        <vxe-column
          field="action"
          title="操作"
          align="center"
          class-name="small-padding fixed-width"
          min-width="180px"
          fixed="right"
        >
          <template #default="{ row }">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(row)"
              v-hasPermi="['logistics:logisticsTracking:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(row)"
              v-hasPermi="['logistics:logisticsTracking:remove']"
              >删除</el-button
            >
          </template>
        </vxe-column>
      </vxe-table>
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- <PaginationEs
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      layout="total, sizes, prev, pager, next,slot"
      @pagination="getList"
    /> -->

    <!-- 添加或修改物流追踪对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="Shipment ID" prop="shipmentId">
          <el-input v-model="form.shipmentId" placeholder="请输入Shipment ID" />
        </el-form-item>
        <el-form-item label="账号标记" prop="account">
          <el-input v-model="form.account" placeholder="请输入账号标记" />
        </el-form-item>
        <el-form-item label="发货日期" prop="sendDate">
          <el-date-picker
            clearable
            v-model="form.sendDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发货日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="体积重除数" prop="volumeRatio">
          <el-input v-model="form.volumeRatio" placeholder="请输入体积重除数" />
        </el-form-item>
        <el-form-item label="渠道" prop="channel">
          <el-input v-model="form.channel" placeholder="请输入渠道" />
        </el-form-item>
        <el-form-item label="总箱数" prop="totalBoxNum">
          <el-input v-model="form.totalBoxNum" placeholder="请输入总箱数" />
        </el-form-item>
        <el-form-item label="方数" prop="squareNum">
          <el-input v-model="form.squareNum" placeholder="请输入方数" />
        </el-form-item>
        <el-form-item label="重量" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入重量" />
        </el-form-item>
        <el-form-item label="units" prop="units">
          <el-input v-model="form.units" placeholder="请输入units" />
        </el-form-item>
        <el-form-item label="货值" prop="price">
          <el-input v-model="form.price" placeholder="请输入货值" />
        </el-form-item>
        <el-form-item label="仓库代号" prop="warehouseCode">
          <el-input v-model="form.warehouseCode" placeholder="请输入仓库代号" />
        </el-form-item>
        <el-form-item label="实际物流运费" prop="logisticsPrice">
          <el-input
            v-model="form.logisticsPrice"
            placeholder="请输入实际物流运费"
          />
        </el-form-item>
        <el-form-item label="平均单价" prop="aveUnitPrice">
          <el-input v-model="form.aveUnitPrice" placeholder="请输入平均单价" />
        </el-form-item>
        <el-form-item label="开船日期" prop="sailDate">
          <el-date-picker
            clearable
            v-model="form.sailDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开船日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="到港日期" prop="arrivalDate">
          <el-date-picker
            clearable
            v-model="form.arrivalDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择到港日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="签收时间" prop="signForDate">
          <el-date-picker
            clearable
            v-model="form.signForDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择签收时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="时效" prop="ageing">
          <el-input v-model="form.ageing" placeholder="请输入时效" />
        </el-form-item>
        <el-form-item label="运单号" prop="waybillNum">
          <el-input v-model="form.waybillNum" placeholder="请输入运单号" />
        </el-form-item>
        <el-form-item label="船名" prop="shipsName">
          <el-input v-model="form.shipsName" placeholder="请输入船名" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="上架数量" prop="groundingNum">
          <el-input v-model="form.groundingNum" placeholder="请输入上架数量" />
        </el-form-item>
        <el-form-item label="删除状态 0-未删除 1-已删除" prop="isDelete">
          <el-input
            v-model="form.isDelete"
            placeholder="请输入删除状态 0-未删除 1-已删除"
          />
        </el-form-item>
        <el-form-item label="创建人ID" prop="createUserId">
          <el-input v-model="form.createUserId" placeholder="请输入创建人ID" />
        </el-form-item>
        <el-form-item label="创建人名称" prop="createUserName">
          <el-input
            v-model="form.createUserName"
            placeholder="请输入创建人名称"
          />
        </el-form-item>
        <el-form-item label="更新人ID" prop="updateUserId">
          <el-input v-model="form.updateUserId" placeholder="请输入更新人ID" />
        </el-form-item>
        <el-form-item label="更新人ID" prop="updateUserName">
          <el-input
            v-model="form.updateUserName"
            placeholder="请输入更新人ID"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <importExcel
      :open.sync="upload.open"
      :title="upload.title"
      :url="upload.url"
      :templateUrl="upload.templateUrl"
      :isUploading="upload.isUploading"
      @readList="getList"
    ></importExcel>
  </div>
</template>

<script>
import {
  listLogisticsTracking,
  getLogisticsTracking,
  delLogisticsTracking,
  addLogisticsTracking,
  updateLogisticsTracking,
  eslistLogisticsTracking,
  getScrollToPage,
} from "@/api/logistics/logisticsTracking";
import { getToken } from "@/utils/auth";
// 导入 Mixin
import { draggableScrollMixin } from "@/utils/draggableScrollMixin"; // 确保路径正确

export default {
  name: "LogisticsTracking",
  // 混入
  mixins: [draggableScrollMixin],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物流追踪表格数据
      logisticsTrackingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shipmentId: null,
        account: null,
        sendDate: null,
        volumeRatio: null,
        channel: null,
        totalBoxNum: null,
        squareNum: null,
        weight: null,
        units: null,
        price: null,
        warehouseCode: null,
        logisticsPrice: null,
        aveUnitPrice: null,
        sailDate: null,
        arrivalDate: null,
        signForDate: null,
        ageing: null,
        waybillNum: null,
        paymentStatus: null,
        shipsName: null,
        groundingNum: null,
        isDelete: null,
        status: null,
        createUserId: null,
        createUserName: null,
        updateUserId: null,
        updateUserName: null,
        searchAfterValuesBase64: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sailDate: [
          { required: true, message: "开船日期不能为空", trigger: "blur" },
        ],
      },
      // 物流追踪导入参数
      upload: {
        // 是否显示弹出层（物流追踪导入）
        open: false,
        // 弹出层标题（物流追踪导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url:
          process.env.VUE_APP_BASE_API +
          "/logistics/logisticsTracking/importData",
        //导出模板地址
        templateUrl: "logistics/logisticsTracking/importTemplate",
      },
    };
  },
  computed: {
    computedPageCount() {
      var pagerCount = Math.ceil(10000 / this.queryParams.pageSize);
      return pagerCount;
    },
  },
  created() {
    console.log();
    this.getList();
    // this.getEsList()
  },
  mounted() {
    // 生成测试数据
    // this.generateTestData(20, 30); // 20行 30列
    // 初始化拖拽滚动功能
    this.$nextTick(() => {
      this.setupDraggableScroll(); // 调用 Mixin 的初始化方法
    });
  },
  beforeDestroy() {
    // 清理拖拽滚动功能
    this.teardownDraggableScroll(); // 调用 Mixin 的清理方法
  },
  methods: {
    /** 查询物流追踪列表 */
    getList() {
      this.loading = true;
      // listLogisticsTracking(this.queryParams).then((response) => {
      //   this.logisticsTrackingList = response.rows;
      //   this.total = response.total;
      //   this.loading = false;
      // });
      eslistLogisticsTracking(this.queryParams).then((response) => {
        this.logisticsTrackingList = response.rows;
        this.total = response.total;
        this.queryParams.searchAfterValuesBase64 =
          response.searchAfterValuesBase64;
        this.loading = false;
        this.getScrollToPageInfo();
      });
    },
    getScrollToPageInfo() {
      getScrollToPage(this.queryParams).then((res) => {
        console.log("数据", res);
      });
    },
    //   getEsList() {
    //   this.loading = true;
    //   eslistLogisticsTracking(this.queryParams).then((response) => {
    //     this.logisticsTrackingList = response.rows;
    //     this.total = response.total;
    //     this.loading = false;
    //   });
    // },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        shipmentId: null,
        account: null,
        sendDate: null,
        volumeRatio: null,
        channel: null,
        totalBoxNum: null,
        squareNum: null,
        weight: null,
        units: null,
        price: null,
        warehouseCode: null,
        logisticsPrice: null,
        aveUnitPrice: null,
        sailDate: null,
        arrivalDate: null,
        signForDate: null,
        ageing: null,
        waybillNum: null,
        paymentStatus: null,
        shipsName: null,
        remark: null,
        groundingNum: null,
        createTime: null,
        isDelete: null,
        status: null,
        createUserId: null,
        createUserName: null,
        updateTime: null,
        updateUserId: null,
        updateUserName: null,
      };
      this.resetForm("form");
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "物流追踪导入";
      this.upload.open = true;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物流追踪";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getLogisticsTracking(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物流追踪";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateLogisticsTracking(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLogisticsTracking(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除物流追踪编号为"' + ids + '"的数据项？')
        .then(function () {
          return delLogisticsTracking(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "logistics/logisticsTracking/export",
        {
          ...this.queryParams,
        },
        `logisticsTracking_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped>
.table-container {
  border: 1px solid #ebeef5;
  border-radius: 4px;
  overflow: hidden;
  position: relative;
  cursor: grab; /* 默认光标显示可抓取 */
}
.table-container:active {
  cursor: grabbing; /* 按下时显示抓取中 */
}
</style>